import Common from "../../../framework/utils/Common";

class CocosLog {
    private cocosAny: any = null;

    constructor() {
        // @ts-ignore
        this.cocosAny = window.cocosAnalytics || false;
    }

    public onPaused() {
        if (this.cocosAny) {
            this.cocosAny.onPause();
        }
    }

    public onResume() {
        if (this.cocosAny) {
            this.cocosAny.onResume();
        }
    }

    public enableDebug(enable: boolean) {
        if (this.cocosAny) {
            this.cocosAny.enableDebug(enable);
        }
    }

    public logStart() {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.loginStart();
        }
    }

    public logSuccess(uId: string) {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.loginSuccess({userID: uId})
        }
    }

    public logFailed() {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.loginFailed();
        }
    }

    public logout(uId: string) {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.logout({userID: uId});
        }
    }

    public setAccountType(type: string) {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.setAccountType(type);
        }
    }

    public setAge(age: number) {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.setAge(age);
        }
    }

    public setGender(gender: number) {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.setGender(gender);
        }
    }

    public setLevel(level: number) {
        if (this.cocosAny) {
            this.cocosAny.CAAccount.setLevel(level);
        }
    }

    public log(event: string, args?: any) {
        if (this.cocosAny) {
            const d = {eventName: event};
            if (args) {
                Common.assign(d, args);
            }
            this.cocosAny.CAEvent.onEvent(d)
        }
        console.log("CocosLog:", event, args);
    }

    public timeStart(event: string) {
        if (this.cocosAny) {
            this.cocosAny.CAEvent.onEventStart({
                eventName: event
            })
        }
        console.time(event);
    }

    public timeEnd(event: string) {
        if (this.cocosAny) {
            this.cocosAny.CAEvent.onEventEnd({
                eventName: event
            })
        }
        console.timeEnd(event);
    }
}

export const cocosLog = new CocosLog();